import 'package:flutter/foundation.dart';
import '../entities/activity_entity.dart';

/// 🎯 活动列表响应模型
/// 
/// 封装后端返回的活动列表数据
/// 对应接口: POST /activities/nearby/
class ActivityListResp {
  final String status;
  final String? message;
  final List<ActivityEntity> activities;
  final Map<String, dynamic>? pagination;
  
  ActivityListResp({
    required this.status,
    this.message,
    required this.activities,
    this.pagination,
  });
  
  /// 是否成功
  bool get isSuccess => status == 'success';
  
  /// 活动数量
  int get count => activities.length;
  
  /// 从JSON创建实例
  factory ActivityListResp.fromJson(Map<String, dynamic> json) {
    try {
      final List<dynamic> dataList = json['data'] as List<dynamic>? ?? [];
      final activities = dataList
          .map((item) => ActivityEntity.fromJson(item as Map<String, dynamic>))
          .toList();
      
      return ActivityListResp(
        status: json['status'] as String? ?? 'unknown',
        message: json['message'] as String?,
        activities: activities,
        pagination: json['pagination'] as Map<String, dynamic>?,
      );
    } catch (e, stackTrace) {
      debugPrint('❌ ActivityListResp.fromJson 解析失败: $e');
      debugPrint('📍 JSON数据: $json');
      debugPrint('📍 错误堆栈: $stackTrace');
      rethrow;
    }
  }
  
  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'status': status,
      'message': message,
      'data': activities.map((e) => e.toJson()).toList(),
      'pagination': pagination,
    };
  }
  
  @override
  String toString() {
    return 'ActivityListResp{status: $status, count: $count, message: $message}';
  }
}

